package com.jianzhile.jobs.base;

import android.app.Activity;
import android.app.Application;
import android.text.TextUtils;

import com.google.gson.Gson;
import com.jianzhile.jobs.bean.User;
import com.jianzhile.jobs.utils.CommonUtil;
import com.jianzhile.jobs.utils.Const;
import com.jianzhile.jobs.utils.HttpUtil;
import com.jianzhile.jobs.utils.SharePreUtil;

import java.util.LinkedList;

/**
 * Created by wutairui on 16/1/14.
 */
public class MyApplication extends Application{

    public LinkedList<Activity> activitys = new LinkedList<Activity>();
    public static final String NAME = CommonUtil.getCurrentDateString() + ".txt";
    public User mUser = new User();
    public boolean isLogin = false;


    private static final MyApplication mInstance = new MyApplication();

    public static MyApplication getInstance(){ return mInstance;}


    @Override
    public void onCreate() {
        super.onCreate();
        HttpUtil.getInstance().initHttpUtil(this);
        SharePreUtil.getInstance().init(this);
        CommonUtil.getInstance().init(this);
        initUser();
       // Fresco.initialize(getApplicationContext());
    }


    private void initUser() {
        String userStr = SharePreUtil.getInstance().getString(Const.USER);
        if (!TextUtils.isEmpty(userStr)) {
            Gson gson = new Gson();
            mUser = gson.fromJson(userStr, User.class);
            isLogin = true;
        }
    }


    /**
     * 添加activity到LinkedList集合
     *
     * @param activity
     */
    public void addActivity(Activity activity) {
        activitys.add(activity);

    }

    /**
     * 退出集合所有的activity
     */
    public void exit() {
        try {
            for (Activity activity : activitys) {
                if (activity != null) {
                    activity.finish();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
        System.gc();
    }


}
